//
// Created by liwen on 2023/12/15.
//

#ifndef EVENT_GROUP_H
#define EVENT_GROUP_H

#include <atomic>
#include <future> // NOLINT

// 等待线程池中多个任务执行完成
class EventGroup;

class Defer{
    using DeferCB = std::function<void()>;
public:
    Defer(DeferCB cb);
    ~Defer();
private:
    DeferCB cb_;
};

class EventGroup final {

public:
     static std::shared_ptr<EventGroup> createEventGroup();

    ~EventGroup();

    void init(int count);

    void signalOne();

    void waitAll();

private:
    EventGroup();
private:
    friend Defer;
    std::atomic<int> count_{0};
    std::shared_ptr<std::promise<void>> promise_;
};

//std::shared_ptr<EventGroup> createEventGroup();

#endif //CPPTEST_EVENTGROUP_H
